// pages/find/index.js
var t = getApp(),
  c = t.requirejs("api/index"),
  a = t.requirejs("core");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    route: "find",
    menu: t.tabBar,
    menu_show: false,
    loaded: false,
    list: [],
    page: 1,
    class_id: '0',
    cate_index: '',
    cate: [],
    class_style: 2,
    isvip:false,
    pay_group:[],
    this_group_id:-1,
    this_group_name:"全部"
  },
  //底部导航跳转
  menu_url: function (k) {
    a.menu_url(k, 2);
  },

  onLoad: function (e) {
    var that = this;
    a.setting();
    if (e != null && e != undefined) {
      that.setData({
        tabbar_index: e.tabbar_index ? e.tabbar_index : -1
      });
    }
      that.setData({
      menu: getApp().tabBar,
      class_id: e.id ? e.id : '0',
    })

      if (that.data.tabbar_index >= 0) {
        that.setData({
        showtabbar: true,
      })
    }
      that.getlist();
      that.initCategory();
    getApp().userlog(1, '付费首页', 'paycontent_browse');
  },
  
  cate_select: function (t) {
    console.log(t,"mm");
    var that = this,
      cate = that.data.cate,
      data = a.pdata(t),
      e = {};
    console.log(data)
    wx.setNavigationBarTitle({
      title: data.name
    })
    e['cate_index'] = data.index;
    e['class_id'] = data.id+"";
    e.list = [];
    e.page = 1;
    e.loaded = false;
    console.log(e,"bb");
    that.setData(e)
    that.getlist('');
  },

  get_list:function(m){
    console.log(t, "aa");
    var that = this,
      group = that.data.group,
      data = a.pdata(t),
      a = {};

    wx.setNavigationBarTitle({
      title: data.name
    })
    a['group_index'] = data.index;
    a['group_id'] = data.id + "";
    a.list = [];
    a.page = 1;
    a.loaded = false;

    that.setData(a)
    that.getlist('');
  },

  initCategory: function () {
    var t = this;
    a.get("paycontent/paycontentClass", {}, function (a) {
      t.setData({
        cate: a.info,
      })
    })
  },

  onShow: function () {
    this.checkuser();
  },

  checkuser: function () {
    var that = this;
    var e = {};
    a.get("paycontent/checkuser", {
      uid: t.getCache('userinfo').uid,
    }, function (t) {
      if (t.code == 0) {
        e.isvip = t.info;
        that.setData(e);
      } else {
        a.alert(t.msg);
      }
    }, true)
  },
  getlist: function (az) {
    var that = this,
      class_id = that.data.class_id,
      page = that.data.page;
    var cz = that.data.this_group_id,nname=that.data.this_group_name;
    if(az!='' && az!=void 0 && az!=null){
      cz = this.data.pay_group[az.detail.value]['id'];
      nname = this.data.pay_group[az.detail.value]['name'];
      class_id=0;
    }
    console.log(cz);
    if (cz != that.data.this_group_id){
      page=1;
      that.setData({
        page:1,
        list:[],
        loaded:false
      })
    }
    that.setData({
      class_id: class_id,
      this_group_id:cz,
      this_group_name: nname
    })
    var e = {};
    a.get("paycontent/paycontent_list", {
      page: page,
      class_id: class_id,
      this_group_id: that.data.this_group_id,
      uid: t.getCache('userinfo').uid,
    }, function (t) {
      console.log(t);
      if (t.code == 0) {
        
        if (t.info) {
          t.info.length > 0 && (e.page = page + 1, e.list = that.data.list.concat(t.info), t.info.length < 10 && (e.loaded = true));
          t.info.length == 0 && (e.loaded = true);
          e.show = true;
          e.pay_group=t.pay_group;
        } else {
          e.loaded = true;
          e.show = true;
        }
        that.setData(e);
        console.log(that.data.loaded);
      } else {
        a.alert(t.msg, function () {
          return;
        })
        return;
      }
      
    }, true)

  },
  change_group:function(va){
    var ta=this;
  },
  /**
    * 下拉刷新
    */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      page: 1,
      loaded: false
    })
    this.getlist('');
    this.initCategory();
    wx.stopPullDownRefresh();
  },
  /**
   *上拉加载
   */
  onReachBottom: function () {
    console.log('加载更多')
    this.data.loaded || this.getlist('')
  },
  to_url: function (e) {
    var data = a.pdata(e), url = '';
    if (data.link) {
      url = '/yb_mingpian/pages/web/index?url=' + escape(data.link) + '&name=' + data.name;
    } else {
      url = '/yb_mingpian/pages/find_info/index?id=' + data.id;
    }
    a.jump(url);
  },

})